package com.aier.admin.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.eicp.ghostboy.futil.RandomUtil;

import org.aspectj.util.FileUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.AdminParenting;
import com.aier.admin.service.IAdminParentingService;
import com.aier.common.common.Constant;
import com.aier.common.common.SystemConfigStatic;
import com.aier.common.dao.ICommonDAO;
import com.aier.proxy.service.IProxyService;
import com.aier.user.common.UserFinalPageSize;

@Scope("prototype")
@Component("adminParentingAction")
public class AdminParentingAction extends AdminBaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name = "adminParentingService")
    private IAdminParentingService adminParentingService;
    //页数
    private int page;
    // 每页显示记录数
    private int page_size;
    // 总页数
    private int page_count;
    // 总数
    private int count;
    //错误码
    private int s;
    
    // 排序方式
    private String sord;
    // 排序字段
    private String sort;
    // 育儿内容列表
    private List<AdminParenting> parentingList;
    // 宝宝天数
    private Long days;
    // 育儿内容
    private String content;
    
    private Long spid;
    
    // 育儿内容
    private AdminParenting parentingInfo;
    // 图片文件
    private File file;
    
    private String se;
    
    //育儿图片的url
    private String imgUrl = SystemConfigStatic.parenting_get_url;
    
    private String filename;
    
    @Override
    public String execute(){
        return SUCCESS;
    }
    
    @SuppressWarnings("unchecked")
    public String searchParenting() {
        if (page <= 0) {
            page = 1;
        }
        int sortable=-1;
        if("days".equals(sort)){
            sortable = ("asc".equals(sord))?IProxyService.ENTRANCE_SORTABLE_SN:IProxyService.ENTRANCE_SORTABLE_SN_DESC;
        }
        Map<String, Object> map = null;
        map = adminParentingService.searchParenting(page, UserFinalPageSize.USERIC_ICARD_PAGESIZE, sortable);
        parentingList = (List<AdminParenting>) map.get(ICommonDAO.OBJ_LIST);
        if (null == parentingList) {
            s = 0;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        s = 1;
        return SUCCESS;
    }
    
    public String create() {
        s = adminParentingService.insertParenting(days, content,filename);
        return SUCCESS;
    }
    
    public String showParentingInfo() {
        parentingInfo = adminParentingService.searchPatentingInfo(spid);
        s = 1;
        return SUCCESS;
    }
    
    public String update() {
        s = adminParentingService.updateParenting(spid, days, content, filename);
        return SUCCESS;
    }
    
    public String uploadImage() {
        if (file == null) {
            s = 0;
            se = "请选择一张图片";
            return SUCCESS;
        }
        
        filename = filename.split("\\.")[0]
                + RandomUtil.genRandomNum(Constant.num_8, RandomUtil.PWD_TYPE_NUM_AND_CHAR) + "." +filename.split("\\.")[1];
        try {
            FileUtil.copyFile(file, new File(SystemConfigStatic.parenting_upload_url+filename));
            s = 1;
        } catch (Exception e) {
            e.printStackTrace();
            se = e.getLocalizedMessage();
            s = 0;
        }
        return SUCCESS;
        
    }
    

    public int getPage() {
        return page;
    }


    public void setPage(int page) {
        this.page = page;
    }


    public int getPage_size() {
        return page_size;
    }


    public int getPage_count() {
        return page_count;
    }


    public int getCount() {
        return count;
    }


    public int getS() {
        return s;
    }

    public void setAdminParentingService(
            IAdminParentingService adminParentingService) {
        this.adminParentingService = adminParentingService;
    }

    public void setSord(String sord) {
        this.sord = sord;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public List<AdminParenting> getParentingList() {
        return parentingList;
    }

    public void setParentingList(List<AdminParenting> parentingList) {
        this.parentingList = parentingList;
    }

    public Long getDays() {
        return days;
    }

    public void setDays(Long days) {
        this.days = days;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Long getSpid() {
        return spid;
    }

    public void setSpid(Long spid) {
        this.spid = spid;
    }

    public AdminParenting getParentingInfo() {
        return parentingInfo;
    }

    public void setParentingInfo(AdminParenting parentingInfo) {
        this.parentingInfo = parentingInfo;
    }

    public void setFile(File file) {
        this.file = file;
    }
    
    public String getSe() {
        return se;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public String getFilename() {
        return filename;
    }


}
